/**
 * @Description:
 * @Author: CHEHSHUANG
 * @Date: 2019/2/18
 */
/**
 * @Description:
 * @Author: CHEHSHUANG
 * @Date: 2019/2/16
 */
import React, {Component} from 'react';
import TransferTable from "../../../commons/components/TransferTable";
import {listAssigned, listUnAssigned, insertFeatureRoles, removeFeatureRoles} from "./FeatureRoleService";
import {hide, show} from "../../../configs/SharedReducer";
import connect from "react-redux/es/connect/connect";

class ConfigRoleTransfer extends Component {
  handleLeftClick = async (rows) => {
    const {roleId} = this.props;
    let ids = [];
    for (let i = 0; i < rows.length; i++) {
      ids.push(rows[i].id);
    }
    await removeFeatureRoles({parentId: roleId, childIds: ids}).then((data) => {
    }).catch(err => {
      this.props.hide()
    })

  }

  handleRightClick = async (rows) => {
    const {roleId} = this.props;
    let ids = [];
    for (let i = 0; i < rows.length; i++) {
      ids.push(rows[i].id);
    }
    await insertFeatureRoles({parentId: roleId, childIds: ids}).then((data) => {
    }).catch(err => {
      this.props.hide()
    })
  };

  leftService = async(params) => {
    const {roleId} = this.props;
    let result=[]
    await listUnAssigned(roleId).then((res) => {
      result=
        res.map(item=>{
        item.appModuleName=item.featureGroup.appModule.name;
        return item;
      })
    });
    return result?result:[]
  };

  rightService = async(params) => {
    let result = []
    const {roleId} = this.props;
    await listAssigned(roleId).then((res) => {
      result=
        res.map(item=>{
        item.appModuleName=item.featureGroup.appModule.name
        return item;
      })
    });
  return result?result:[]
  };

  render() {
    const columns = [
      {
        title: '代码',
        dataIndex: 'code',
      },
      {
        title: '应用模块',
        dataIndex: 'featureGroup.appModule.name',
      },
      {
        title: '名称',
        dataIndex: 'name',
      }
    ];
    return (
      <TransferTable
        style={{padding: 8, background: "#F0F2F5"}}
        handleLeftClick={this.handleLeftClick}
        handleRightClick={this.handleRightClick}
        searchRightKey={["code","name","appModuleName"]}
        searchLeftKey={["code","name","appModuleName"]}
        //  searchTableConfig={searchTableConfig}
        rightService={this.rightService}
        leftService={this.leftService}
        leftColumns={columns}
        rightColumns={columns}
        // onRef={this.onTableRef}
      />
    );
  }
}

const mapStateToProps = (state) => {
  return {};
}

const mapDispatchToProps = (dispatch) => {
  return {
    show: () => {
      dispatch(show())
    },
    hide: () => {
      dispatch(hide())
    }
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(ConfigRoleTransfer)
